package youleche.com.business;

import java.sql.Connection;

import youleche.com.common.IDFactory;
import youleche.com.connection.ConnectionFactory;
import youleche.com.factory.DaoFactory;
import youleche.com.idao.IEmailModule;
import youleche.com.model.MEmailModule;

/**
 * 邮件模板业务类
 * @author Administrator
 *
 */
public class BEmailModule {
	/**
	 * 数据库连接
	 */
	private Connection con = null;
	
	/**
	 * 处理器
	 */
	private IEmailModule handler = null;
	
	/**
	 * 构造函数
	 */
	public BEmailModule(){
		this.handler = DaoFactory.getDEmailModule();
	}
	
	/**
	 * 添加邮件模板
	 * @param emailModule 邮件模板
	 * @return 返回添加影响的行数
	 * @throws Exception 异常
	 */
	public int addEmailModule(MEmailModule emailModule)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheWriteConnection();
			emailModule.setId(IDFactory.getId("meileche", "emailModule"));
			return this.handler.addEmailModule(this.con, emailModule);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 得到邮件模板通过模板名（模板名唯一）
	 * @param moduleName 模板名
	 * @return 返回模板信息
	 * @throws Exception 异常
	 */
	public MEmailModule getEmailModuleByModuleName(
			String moduleName) throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getEmailModuleByModuleName(this.con, moduleName);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 更新模板信息
	 * @param emailModule 模板信息
	 * @return 返回更新的行数
	 * @throws Exception 异常
	 */
	public int updateEmailModuleByModuleName(
			MEmailModule emailModule) throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheWriteConnection();
			return this.handler.updateEmailModuleByModuleName(this.con, emailModule);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
}
